package com.iplant.mes.controller.print;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.SFCFrockBindService;
import com.iplant.mes.service.SFCService;
import com.iplant.mes.service.po.print.SFCFactoryTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 出厂试验打印控制器
 *
 * @author YouWang·Peng
 * 2023-8-30 11:26:33
 */
@Slf4j
@RestController
@RequestMapping("/api/SFCFactoryTest")
public class SFCFactoryTestController extends BaseController {

    public SFCFactoryTestController() {
        super();
    }

    @Autowired
    SFCService mSFCService;

    @Autowired
    SFCFrockBindService mSFCFrockBindService;

    /**
     * 查询出厂试验单数据
     */
    @GetMapping("/QueryFactoryTest")
    public Object QueryFactoryTest(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String wWorkpieceNos = StringUtils.parseString(request.getParameter("WorkpieceNos"));
            if (StringUtils.isEmpty(wWorkpieceNos)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<SFCFactoryTest> wServiceResult = mSFCService.SFC_QueryFactoryTest(wLoginUser, wWorkpieceNos);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("QueryFactoryTest", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    /**
     * 导出出厂试验单
     */
    @PostMapping("/Export")
    public Object Export(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            SFCFactoryTest wSFCFactoryTest = CloneTool.Clone(wParam.get("data"), SFCFactoryTest.class);
            if (wSFCFactoryTest == null) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

//            ServiceResult<String> wServiceResult = mSFCService.SFC_ExportFactoryTest(wLoginUser, wSFCFactoryTest);
            ServiceResult<String> wServiceResult = mSFCFrockBindService.SFC_ExportFactoryTest_V1(wLoginUser,
                    wSFCFactoryTest);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("Export", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
